<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="附件类型">
          <a-select placeholder="请输入附件类型" v-decorator="['FilesTypeId', validatorRules.filestypeid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in filesTypeData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="附件名">
          <a-upload
            name="file"
            :multiple="false"
            :accept="accept"
            :showUploadList="false"

            v-decorator="['FilesName', {}]"
            :beforeUpload="uploadPic">
            <a-button><a-icon type="upload"/>上传</a-button>
          </a-upload>
          <span style="margin-left:20px;" v-for="(item,index) in fileList">{{item.name}}<a-icon style="cursor:pointer;margin-left:20px;" type="delete" @click="deleteFileObj(index)"/></span>
        </a-form-item>
		
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyFilesModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        accept: '*',
        fileList:[],
        filesTypeData:[{
          id:'80',
          name:'BP'
        },{
          id:'81',
          name:'公司简介'
        },{
          id:'82',
          name:'其它'
        }],
        companyData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companyfilesid:{rules: [{ required: true, message: '请输入公司附件ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        filestypeid:{rules: [{ required: true, message: '请输入附件类型!' }]},
        filesname:{rules: [{ required: true, message: '请输入附件名!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        url: {
          add: "/companyFiles/add",
          edit: "/companyFiles/update",
          baseList:"/baseInfo/queryRows",
          updateFile:"/oss/upload"
        },
      }
    },
    created () {
      // this.getCompany();
      this.getBaseData();
    },
    methods: {
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'FileType'
            }]}).then((res)=>{
          if(res.status == 1){
            this.filesTypeData = res.data.rows;
          }
        })
      },
      deleteFileObj(index){
        this.fileList = [];
        this.form.setFieldsValue({
            FilesName: '',
        })
      },
      uploadPic(fileArr){
        if(!this.url.updateFile){
          return false;
        }
        let httpurl = this.url.updateFile;
        let formData = new FormData();
        // for(let i=0;i<fileArr.length;i++){
        //   formData.append('file',fileArr[i])
        // }
        formData.append('file',fileArr)
        let that = this;
        this.fileList = [];
        that.form.setFieldsValue({
            FilesName: '',
        })
        this.$http.postAction(httpurl,formData).then((res)=>{
          if(res.name){
            that.$message.success('上传成功');
            that.form.setFieldsValue({
                    FilesName: JSON.stringify(res),
                })
            this.fileList.push({name:res.name})
            console.log('ssss',this.fileList)
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
          // that.close();
        })
        return false;
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.FilesTypeId = this.model.FilesType;
        this.model.CompanyId = this.model.Company;
        this.fileList = [];
        if(this.model.FilesName){
          let name = this.model.FilesName?JSON.parse(this.model.FilesName).name:''
          let fileObj ={
            name:name
          }
          this.fileList.push(fileObj)
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyId','FilesTypeId','FilesName','Company_CompanyName'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyFilesId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>